package cn.marinda.resturants.service.impl;

import cn.marinda.resturants.entity.Member;
import cn.marinda.resturants.entity.User;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.mapper.MemberMapper;
import cn.marinda.resturants.service.MemberService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/** 会员服务层实现类
 *
 * @author hwj
 */
@Service("memberService")
public class MemberServiceImpl  extends ServiceImpl<MemberMapper,Member>  implements MemberService {


    /**
     * 选择uid成员
     *
     * @param uid uid
     * @return {@link Member}
     */
    @Override
    public Member selectMemberUid(String uid) {
        return baseMapper.selectById(uid);
    }

    /**
     * 会员列表
     *
     * @return 会员列表
     */
    @Override
    public List<Member> queryList() {
        return baseMapper.selectList(null);
    }

    /**
     * 添加会员
     *
     * @param member 会员
     * @version 1.0
     * @data 2021/12/2
     */
    @Override
    public boolean addMember(Member member) {
        return baseMapper.insert(member)>0;
    }

    /**
     * 删除会员
     *
     * @param uid 会员
     * @return 结果
     */
    @Override
    public boolean deleteMember(Integer uid) {
        return baseMapper.deleteById(uid)>0;
    }

    /**
     * 修改会员
     *
     * @param member 会员
     * @return 结果
     */
    @Override
    public boolean updateMember(Member member) {
        Member sqlMember = baseMapper.selectById(member.getUid());
        if(sqlMember == null){
            return false;
        }
        member.setVersion(sqlMember.getVersion());

        return lambdaUpdate().eq(Member::getUid,member.getUid()).eq(Member::getVersion,member.getVersion()).set(Member::getVersion,member.getVersion()+1).update(member);
    }

    /**
     * 查询指定会员的折扣
     *
     * @param uid uid
     * @return 折扣
     */
    @Override
    public BigDecimal selectDiscount(Integer uid) {
        return baseMapper.selectDiscount(uid);
    }

    /**
     * 查询会员数量
     *
     * @param uid uid
     * @return 数量
     */
    @Override
    public Integer selectCount(Integer uid) {
        return baseMapper.selectCount(uid);
    }

    /**
     * 查询会员礼券数
     *
     * @param uid 会员uid
     * @return 礼券数
     */
    @Override
    public BigDecimal selectVochers(Integer uid) {
        return baseMapper.selectVochers(uid);
    }

    /**
     * 查询会员名称
     *
     * @param uid uid
     * @return 会员名称
     */
    @Override
    public String selectName(Integer uid) {
        return baseMapper.selectName(uid);
    }

    /**
     * 查询会员创建日期
     *
     * @param uid uid
     * @return 会员创建日期
     */
    @Override
    public String selectCreateDate(Integer uid) {
        return baseMapper.selectCreateDate(uid);
    }

    /**
     * 分页
     *
     * @param pages 分页
     * @return 分页结果
     */
    @Override
    public IPage<Member> selectMemberPage(Pages<?> pages) {
        IPage<Member> iPage = baseMapper.selectMemberPage(pages);
        return iPage;
    }
}
